﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using net.windward.utils.ado;
using System.Collections.ObjectModel;

namespace ArrowScheduleCore
{
    /// <summary>
    /// A single variable set for a scheduled event. This has a name, a value, and for a date, a calender offset.
    /// </summary>
    public class ScheduleVarItem
    {

        /// <summary>
        /// What offset from the date run to set the calendar value to. These have set integer values that are used
        /// in aspx pages - do not change them.
        /// </summary>
        public enum CAL_OFFSET
        {
            /// <summary>
            /// Use the value saved for the map var.
            /// </summary>
            FIXED = 0,
            /// <summary>
            /// Set the value to today.
            /// </summary>
            TODAY = 1,
            /// <summary>
            /// Set the value to the first day of the week we are on. This uses the culture of the server that runs the event.
            /// </summary>
            START_OF_WEEK = 2,
            /// <summary>
            /// Set the value to the first day of the month we are on. This uses the culture of the server that runs the event.
            /// </summary>
            START_OF_MONTH = 3,
            /// <summary>
            /// Set the value to the first day of the quarter we are on. This uses the culture of the server that runs the event.
            /// </summary>
            START_OF_QUARTER = 4,
            /// <summary>
            /// Set the value to the first day of the year we are on. This uses the culture of the server that runs the event.
            /// </summary>
            START_OF_YEAR = 5
        };

        private object value;
        private CAL_OFFSET calOffset;

        /// <summary>
        /// Create a new object.
        /// </summary>
        /// <param name="name">The name of the variable.</param>
        /// <param name="value">The value of the variable. This must be a number (float/double or int/long), DateTime, or string.</param>
        public ScheduleVarItem(string name, object value)
        {
            ScheduleId = WrConnectionParams.NULL_PK;
            Name = name;
            Value = value;
            CalOffset = CAL_OFFSET.FIXED;
        }

        /// <summary>
        /// Create a new object.
        /// </summary>
        /// <param name="name">The name of the variable.</param>
        /// <param name="value">The value of the variable. This must be a number (float/double or int/long), DateTime, or string.</param>
        /// <param name="calOffset">For date variables, what to set the value to. Set to FIXED for non-dates.</param>
        public ScheduleVarItem(string name, object value, CAL_OFFSET calOffset)
        {
            ScheduleId = WrConnectionParams.NULL_PK;
            Name = name;
            Value = value;
            CalOffset = calOffset;
        }

        /// <summary>
        /// Create a new object.
        /// </summary>
        /// <param name="scheduleId">The PK of the schedule this is for.</param>
        /// <param name="name">The name of the variable.</param>
        /// <param name="value">The value of the variable. This must be a number (float/double or int/long), DateTime, or string.</param>
        /// <param name="calOffset">For date variables, what to set the value to. Set to FIXED for non-dates.</param>
        public ScheduleVarItem(long scheduleId, string name, object value, CAL_OFFSET calOffset)
        {
            ScheduleId = scheduleId;
            Name = name;
            Value = value;
            CalOffset = calOffset;
        }

        /// <summary>
        /// The PK of the scheduled event that this belongs to.
        /// </summary>
        public long ScheduleId { get; set; }

        /// <summary>
        /// The name of the variable.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The value of the variable. This must be a number (float/double or int/long), DateTime, or string.
        /// </summary>
        public object Value
        {
            get { return value; }
            set
            {
                this.value = value;
                if ((value != null) && (!(value is DateTime)))
                    calOffset = CAL_OFFSET.FIXED;
            }
        }

        /// <summary>
        /// For date variables, what to set the value to. Set to FIXED for non-dates.
        /// </summary>
        public CAL_OFFSET CalOffset
        {
            get { return calOffset; }
            set
            {
                calOffset = value;
                if (calOffset != CAL_OFFSET.FIXED)
                    Value = null;
            }
        }
    }

    /// <summary>
    /// A collection of ScheduleVarItem objects.
    /// </summary>
    public class ScheduleVarItemCollection : Collection<ScheduleVarItem>
    {
    }
}
